package com.zhangck.bos2_0.action.take_delivery;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.zhangck.bos2_0.Service.take_delivery.PromotionService;
import com.zhangck.bos2_0.common.BaseAction;

import antlr.collections.List;
import cn.itcast.bos.domain.take_delivery.Promotion;

/** 
* @author 上午茶
* @version 创建时间：2018年6月7日 下午3:19:04 
* 类说明 
*/
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class PromotionAction extends BaseAction<Promotion>{

	//固定写法
	private File titleImgFile;
	private String titleImgFileFileName;
	public void setTitleImgFile(File titleImgFile) {
		this.titleImgFile = titleImgFile;
	}
	public void setTitleImgFileFileName(String titleImgFileFileName) {
		this.titleImgFileFileName = titleImgFileFileName;
	}
	
	@Autowired
	private PromotionService  permissionService;
	
	@Autowired
	@Qualifier("jmsQueueTemplate")
	private JmsTemplate jmsTemplate;

	
	@Action(value="promotionFormaction_save",results={@Result(name="success", type="redirect", location="./pages/take_delivery/promotion.html")})
	public String  promotionFormaction_save(){
		try {
	// 根目录路径，可以指定绝对路径，比如 d:/xxx/upload/xxx.jpg
		String rootPath = ServletActionContext.getServletContext().getRealPath(
					"/")+ "upload/";
				// 根目录URL，可以指定绝对路径，比如 http://www.yoursite.com/attached/
		String rootUrl = ServletActionContext.getRequest().getContextPath()
					+ "/upload/";
		// 生成随机图片名
		UUID uuid = UUID.randomUUID();
		String ext = titleImgFileFileName
		.substring(titleImgFileFileName.lastIndexOf("."));
		String randomFileName = uuid + ext;

		// 保存图片 (绝对路径)
		File destFile = new File(rootPath + File.separator + randomFileName);
		System.out.println(destFile.getAbsolutePath());
		FileUtils.copyFile(titleImgFile, destFile);
		
		model.setTitleImg(rootUrl+randomFileName);
		String id = ServletActionContext.getRequest().getParameter("id");
		
		final String falg = "true";
		
		jmsTemplate.send("update",new MessageCreator() {
			@Override
			public Message createMessage(Session session) throws JMSException {
				MapMessage mapMessage = session.createMapMessage();
				mapMessage.setString("id", model.getId()+"");
				mapMessage.setString("falg", falg);
				return mapMessage;
			}
		});
		
		permissionService.Save(model);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
		return "success";
	}

	
	private Integer page;
	private Integer rows;
	public void setPage(Integer page) {
		this.page = page;
	}
	public void setRows(Integer rows) {
		this.rows = rows;
	}
	@Action(value="promotionpage",results={@Result(name="success", type="json")})
	public String promotionpage(){
		Pageable request = new PageRequest(page-1, rows);
		Page<Promotion> page1 = permissionService.findPageData(request);
		System.out.println(page1.getSize());
		pushPageDataValueStack(page1);
		return "success";
	}
	
	

	@Action(value="promtion_findId",results={@Result(name="success", type="json")})
	public String promtion_findId(){
		String id = ServletActionContext.getRequest().getParameter("id");
		int int1 = Integer.parseInt(id);
		System.out.println(id);
		 Promotion promotion = permissionService.findByID(int1);
		 ActionContext.getContext().getValueStack().push(promotion);
		return "success";
	}
}
